package com.ciner.utility;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.ciner.R;

public class MovieListAdapter extends ArrayAdapter<Movie> {
	Context context;
	int layoutResourceId;
	Movie data[] = null;

	public MovieListAdapter(Context context, int layoutResourceId, Movie[] data) {
		super(context, layoutResourceId, data);

		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View row = convertView;
		MovieHolder holder = null;

		if (row == null) {

			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new MovieHolder();

			holder.title = (TextView) row.findViewById(R.id.title);
			holder.year = (TextView) row.findViewById(R.id.year);
			holder.description = (TextView) row.findViewById(R.id.description);
			holder.genre = (TextView) row.findViewById(R.id.genre);
			holder.duration = (TextView) row.findViewById(R.id.duration);

			holder.rating = (RatingBar) row.findViewById(R.id.rating);

			holder.image = (ImageView) row.findViewById(R.id.cover);

		} else {
			holder = (MovieHolder) row.getTag();
		}

		return row;
	}

	public class MovieHolder {

		public TextView year, duration, genre, title, description;

		public ImageView image;

		public RatingBar rating;

	}

}
